/**
 * 
 */
javascript:(
function()
        {
        if (typeof FB == 'undefined') {
                alert('Error1: FB session not active, reload the game (www.mafiawars.com) and unframe again.');
                return; 
        };
        FB.init({
                appId  : '10979261223',
                status : true, // check login status
                cookie : true, // enable cookies to allow the server to access the session
                frictionlessRequests : true
        });
        
        var friends = [];
        var version = 'List FB Friends v1.02';
        var fbid = User.trackId;
        var fql1="SELECT uid,name FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1="+fbid+") AND is_app_user = 1 ORDER BY name";
        var fql2="SELECT uid,name FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1="+fbid+") AND is_app_user = 0 ORDER BY name";
        var fql3="SELECT uid,name FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1="+fbid+") ORDER BY name";
        content = document.getElementById('popup_fodder');

        var config_html = 
        '<style type="text/css">'+
                '.messages img{margin:0 3px;vertical-align:top}'+
                '.messages input {border: 1px solid #FFF;margin 0;padding 0;background: #000; color: #FFF; width: 20px;}'+
                '#play{display:none}'+
                '#pause{display:inline}'+
                '#close{display:inline}'+
        '</style>'+
        '<table class="messages">'+
        '<tr>'+
                '<td colspan="3" align="right" style="text-align:right;font-size:0.8em;">'+version+' - <a href="http://www.spockholm.com/mafia/bookmarklets.php" target="_top">Spockholm Mafia Tools</a> - <a href="http://www.spockholm.com/mafia/donate.php?ListFBFriends" alt="Buy me a beer" target="_blank">Donate</a> - <a href="#" id="close"><img src="http://www.spockholm.com/mafia/stop.gif" title="Close" width="16" height="16"></a></td></tr>'+
        '</tr>'+
        '<tr>'+
                '<td colspan="3">'+
                        'Load: <a id="mwfriends" href="#">MW playing</a> - <a id="nonmwfriends" href="#">Not MW playing</a> - <a id="fbfriends" href="#">All FB</a> - friends.'+
                '</td>'+
        '<tr>'+
                '<td>Status:</td>'+
                '<td id="status" align="left" colspan="2"></td>'+
        '</tr>'+
        '<tr>'+
                '<td colspan="3"><textarea name="friendlist" id="friendlist" class="instructions" rows="15" cols="50">'+
                        'Enter in this box a list of your mafia.\n'+
                '</textarea></td>'+
        '</tr>'+
        '</table>';
        
        //Pistol Pete FQL code
        function parseFBFr(fql){
                //alert(fql);
                var f=function(){
                        var results=[];
                        var friendquicklist;
                        var friendcount = 0;
                        return {
                                getResults: function(){ return results; },
                                process: function(fbfr){
                                        if (fbfr){
                                                for(i=0;i<fbfr.length;i++) {
                                                  //results[results.length]={   'id':fbfr[i].uid, 'name':fbfr[i].name };
                                                        if (fbfr[i].name != null) {
                                                                friendquicklist += fbfr[i].uid+', '+ fbfr[i].name.replace(/[^A-Za-z0-9 ]/g, '') +'\n';
                                                                //friendquicklist += fbfr[i].uid+', '+ fbfr[i].name.replace(/[^A-Za-z0-9 ]/g, '') + 'http://mafiatornado.com/player.php?fbid='+fbfr[i].uid + '\n';
                                                                friendcount++;
                                                                //friends.pop(fbfr[i].uid+', '+fbfr[i].name.replace(/[^A-Za-z0-9 ]/g, ''));
                                                        }
                                                }
                                                //generator.document.write('results.length='+results.length);
                                                //document.getElementById('results').value = ''results.length='+results.length...';
                                                //friends=friendarray.split(',');
                                        }
                                        else { 
                                                friendquicklist='Unable to retrive list. Please try again.';
                                        }
                                        //friend=results;
                                        document.getElementById('friendlist').value = friendquicklist.replace(/undefined/,'');
                                        document.getElementById('status').innerHTML = 'Loaded '+friendcount+' friends';
                                }
                        };
                }();
                document.getElementById('friendlist').value = 'Loading...Be Patient...';
        FB.api({
            method: 'fql.query',
            query: fql
        },
                function (response) {
            f.process(response);
        });        
    //tablepop();      
                return;
        }
        
        function create_div() {
                if(document.getElementById('spockdiv')) {
                        document.getElementById('spockdiv').innerHTML = config_html;
                }
                else {
                        var spock_div=document.createElement("div");
                        spock_div.id = 'spockdiv';
                        spock_div.innerHTML = config_html;
                        content.insertBefore(spock_div, content.firstChild);
                }
        }
        create_div();
        parseFBFr(fql1);

        document.getElementById('mwfriends').onclick=function(e) {
                document.getElementById('friendlist').focus();
                parseFBFr(fql1);
                //generator.document.write('results.length='+results.length);
    //tablepop();      
        };

        document.getElementById('nonmwfriends').onclick=function(e) {
                document.getElementById('friendlist').focus();
                parseFBFr(fql2);
        };
        document.getElementById('fbfriends').onclick=function(e) {
                document.getElementById('friendlist').focus();
                parseFBFr(fql3);
        };
        document.getElementById('close').onclick=function(e) {
                document.getElementById('popup_fodder').removeChild(document.getElementById('spockdiv'));
                return false;
        };
        function dirtypop() {
                var generator=window.open('','name','height=500,width=500');
                generator.document.write('<html><head><title>Your Facebook Friends</title>');
                generator.document.write('</head><body>');
                generator.document.write('<pre>Number of friends in this list: '+friends.length+'</pre><textarea rows="20">');
                for(i = 0; i < friends.length; i++) {
                        generator.document.write(friends[i]+'\n');
                }
                generator.document.write('</textarea><pre><a href="javascript:self.close()">Close</a> the popup.</pre>');
                generator.document.write('</body></html>');
                generator.document.close();
        }
  function tablepop() {
                var generator=window.open('','name','height=500,width=500');
                generator.document.write('<html><head><title>Your Facebook Friends</title>');
                generator.document.write('</head><body>');
                generator.document.write('<pre>Number of friends in this list: '+friendlist.length+'</pre>');
    generator.document.write('<table>'+
    '<tr>'+
                '<th align="left">FB ID</th>'+
                '<th align="left">Name</th>'+
    '</tr>');
                for(i = 0; i < friendlist.length; i++) {
                        generator.document.write(friendlist[i]+'\n');
      generator.document.write('<tr>'+
                  '<td align="left">'+friendlist[i].uid+'</td>'+
                  '<td align="left">'+friendlist[i].name+'</td>'+
      '</tr>');         
                }
    generator.document.write('</table>');
                generator.document.write('<pre><a href="javascript:self.close()">Close</a> the popup.</pre>');
                generator.document.write('</body></html>');
                generator.document.close();
        }       

})()
